<template>
  <div>

    <div style="padding: 5px 0; margin-left: 20px; line-height: 40px">
      <!--  搜索框部分  -->
      <p style="font-size: 16px"><i class="el-icon-search"></i> 查询：
        <el-input style="width: 180px" placeholder="请输入公司名" suffix-icon="el-icon-edit" v-model="name"></el-input>
        <el-input style="width: 180px" placeholder="请输入经营范围" suffix-icon="el-icon-share" class="ml-5"
                  v-model="remarks"></el-input>
        <el-input style="width: 180px" placeholder="请输入地址" suffix-icon="el-icon-message" class="ml-5"
                  v-model="address"></el-input>
        <el-input style="width: 180px" placeholder="请输入联系方式" suffix-icon="el-icon-phone" class="ml-5"
                  v-model="contact"></el-input>
        <el-button type="primary" class="ml-5" @click="load">搜索</el-button>
        <el-button type="warning" style="margin-left: 5px" @click="reset">重置</el-button>
      </p>
    </div>

    <div style="margin: 10px 0; margin-left: 20px">
      <p style="font-size: 16px">
        <i class="el-icon-magic-stick"></i> 操作：
        <!--  按钮部分  -->
        <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
        <el-popconfirm
            class="ml-5"
            confirm-button-text='啊对对对，把你数据库搞崩溃'
            cancel-button-text='还是不了'
            icon="el-icon-info"
            icon-color="red"
            title="你他妈的确定要把这些全删了吗？"
            @confirm="delBatch"
        >
          <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
        </el-popconfirm>
        <el-upload
            action="http://localhost:9090/supplier/import"
            :show-file-list="false"
            accept=".xlsx"
            :on-success="handleExcelImportSuccess"
            style="display: inline-block">
          <el-button type="primary" style="margin-left: 5px">导入 <i class="el-icon-bottom"></i></el-button>
        </el-upload>
        <!--  export是关键字，不能写  -->
        <el-button type="primary" style="margin-left: 5px" @click="exp">导出 <i class="el-icon-top"></i></el-button>

      </p>
    </div>

    <!--  数据部分  -->
    <el-table
        :data="tableData"
        border stripe
        :header-cell-class-name="'headerBg'"
        @selection-change="handleSelectionChange"
        style="margin-top: 30px"
    >
      <el-table-column type="selection" swidth="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="name" label="公司名" width="140"></el-table-column>
      <el-table-column prop="remarks" label="经营范围" width="160"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column prop="contact" label="联系方式"></el-table-column>


      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="warning" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='啊对对对'
              cancel-button-text='还是不了'
              icon="el-icon-info"
              icon-color="red"
              title="你他妈的确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>

      </el-table-column>

    </el-table>

    <!--  分页功能布置  -->
    <div style="padding: 10px 0; margin-top: 20px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        <!--  total后台返回前端，pageNum、pageSize前端返回  -->
      </el-pagination>
    </div>

    <!--  弹窗  -->
    <el-dialog title="公司信息" :visible.sync="dialogFormVisible" width="40%">
      <el-form label-width="100px" size="small">
        <el-form-item label="公司名：">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="经营范围：">
          <el-input v-model="form.remarks" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址：">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系方式：">
          <el-input v-model="form.contact" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "Supplier",
  data() {
    return {
      // 数据填充
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      name: "",
      remarks: "",
      address: "",
      contact: "",
      form: {},
      dialogFormVisible: false,
      multipleSelection: [],
      headerBg: 'headerBg'
    }
  },
  created() {
    // 请求分页查询数据
    this.load()
  },
  methods: {
    load() {
      this.request.get("/supplier/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
          remarks: this.remarks,
          address: this.address,
          contact: this.contact
        }
      }).then(res => {
        console.log(res)

        this.tableData = res.data.records
        this.total = res.data.total

      })
    },
    save() {
      this.request.post("/supplier", this.form).then(res => {
        if (res.data) {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row) {
      this.form = row
      this.dialogFormVisible = true
    },
    del(id) {
      this.request.delete("/supplier/" + id).then(res => {
        if (res.data) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id) // [{},{},{}] => [1,2,3]
      this.request.post("/supplier/del/batch", ids).then(res => {
        if (res) {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.name = ""
      this.remarks = ""
      this.address = ""
      this.contact = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    exp() {
      window.open("http://localhost:9090/supplier/export")
    },
    handleExcelImportSuccess() {
        this.$message.success("文件导入成功！")
        this.load()
    }
  }
}
</script>

<style scoped>
.headerBg {
  background: #eee !important;
}
</style>